/**************************************************************************************************
	$Header: /pub/cvsroot/mydns/src/lib/soa.c,v 1.29 2002/08/31 08:08:00 bboy Exp $

	Copyright (C) 2002  Don Moore <bboy@bboy.net>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at Your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
**************************************************************************************************/

#include "mydns.h"

char mydns_soa_table_name[PATH_MAX] = MYDNS_SOA_TABLE;

/* Optional columns */
int mydns_soa_use_active = 0;
int mydns_soa_use_xfer = 0;


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	MYDNS_SOA_COUNT
	Returns the number of zones in the soa table.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
u_int32_t
mydns_soa_count(mysql)
	MYSQL *mysql;
{
	MYSQL_RES *res = NULL;
	MYSQL_ROW row;
	char query[512];
	size_t querylen;
	u_int32_t rv = 0;

	querylen = snprintf(query, sizeof(query), "SELECT COUNT(*) FROM %s", mydns_soa_table_name);
	if (mysql_real_query(mysql, query, querylen) || !(res = mysql_store_result(mysql)))
		return (-1);
	if ((row = mysql_fetch_row(res)))
		rv = (u_int32_t)strtoul(row[0], (char **)NULL, 10);
	mysql_free_result(res);
	return (rv);
}
/*--- mydns_soa_count() -------------------------------------------------------------------------*/


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	MYDNS_SET_SOA_TABLE_NAME
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void
mydns_set_soa_table_name(name)
	char *name;
{
	if (!name)
		strncpy(mydns_soa_table_name, MYDNS_SOA_TABLE, sizeof(mydns_soa_table_name)-1);
	else
		strncpy(mydns_soa_table_name, name, sizeof(mydns_soa_table_name)-1);
}
/*--- mydns_set_soa_table_name() ----------------------------------------------------------------*/


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	MYDNS_PARSE_SOA
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline MYDNS_SOA *
mydns_parse_soa(row)
	MYSQL_ROW row;
{
	MYDNS_SOA *rv;

	if ((rv = (MYDNS_SOA *)calloc(1, sizeof(MYDNS_SOA))))
	{
		rv->next = NULL;

		rv->id = atou(row[0]);
		strncpy(rv->origin, row[1], sizeof(rv->origin)-1);
		strncpy(rv->ns, row[2], sizeof(rv->ns)-1);
		if (!rv->ns[0])
			snprintf(rv->ns, sizeof(rv->ns), "ns.%s", rv->origin);
		strncpy(rv->mbox, row[3], sizeof(rv->mbox)-1);
		if (!rv->mbox[0])
			snprintf(rv->mbox, sizeof(rv->mbox), "hostmaster.%s", rv->origin);
		rv->serial = atou(row[4]);
		rv->refresh = atou(row[5]);
		rv->retry = atou(row[6]);
		rv->expire = atou(row[7]);
		rv->minimum = atou(row[8]);
		rv->ttl = atou(row[9]);

		/* Make sure TTL for SOA is at least the minimum */
		/* XXX: Update this in the database, maybe */
		if (rv->ttl < rv->minimum)
			rv->ttl = rv->minimum;
	}
	return (rv);
}
/*-----------------------------------------------------------------------------------------------*/


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	MYDNS_SOA_DUP
	Create a duplicate copy of the record.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
MYDNS_SOA *
mydns_soa_dup(start, recurse)
	MYDNS_SOA *start;
	int recurse;
{
	register MYDNS_SOA *first = NULL, *last = NULL, *soa, *s, *tmp;

	for (s = start; s; s = tmp)
	{
		tmp = s->next;

		if ((soa = (MYDNS_SOA *)malloc(sizeof(MYDNS_SOA))))
		{
			soa->id = s->id;
			strncpy(soa->origin, s->origin, sizeof(soa->origin)-1);
			strncpy(soa->ns, s->ns, sizeof(soa->ns)-1);
			strncpy(soa->mbox, s->mbox, sizeof(soa->mbox)-1);
			soa->serial = s->serial;
			soa->refresh = s->refresh;
			soa->retry = s->retry;
			soa->expire = s->expire;
			soa->minimum = s->minimum;
			soa->ttl = s->ttl;
			soa->next = NULL;
			if (recurse)
			{
				if (!first) first = soa;
				if (last) last->next = soa;
				last = soa;
			}
			else
				return (soa);
		}
	}
	return (first);
}
/*--- mydns_dup_soa() ---------------------------------------------------------------------------*/


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	MYDNS_SOA_SIZE
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline size_t
mydns_soa_size(first)
	MYDNS_SOA *first;
{
	register MYDNS_SOA *p;
	register size_t size = 0;

	for (p = first; p; p = p->next)
		size += sizeof(MYDNS_SOA) + strlen(p->origin) + strlen(p->ns) + strlen(p->mbox);

	return (size);
}
/*-----------------------------------------------------------------------------------------------*/



/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	_MYDNS_SOA_FREE
	Frees the pointed-to structure.	Don't call this function directly, call the macro.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
inline void
_mydns_soa_free(first)
	MYDNS_SOA *first;
{
	register MYDNS_SOA *p, *tmp;

	for (p = first; p; p = tmp)
	{
		tmp = p->next;

		free(p);
	}
}
/*-----------------------------------------------------------------------------------------------*/


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	MYDNS_SOA_LOAD
	Returns 0 on success or nonzero if an error occurred.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
int
mydns_soa_load(mysql, rptr, origin)
	MYSQL	*mysql;					/* MySQL connection */
	MYDNS_SOA **rptr;				/* Result list will be returned here */
	u_char *origin;				/* The origin to look up in the `soa' table */
{
	MYDNS_SOA *first = NULL, *last = NULL;
	size_t	 originlen, querylen;
	u_char	 query[DNS_QUERYBUFSIZ];
	u_char	 esc[DNS_MAXNAMELEN + DNS_MAXNAMELEN + 1];
	MYSQL_RES *res;
	MYSQL_ROW row;

	if (rptr) *rptr = NULL;
	MYDNS_CLEARERR(mysql)

	/* Verify args */
	if (!mysql || !origin || !rptr)
	{
		errno = EINVAL;
		return (-1);
	}

	/* Escape `origin' */
	originlen = strlen(origin);
	if (originlen + originlen + 1 > sizeof(esc))
	{
		errno = EINVAL;
		return (-1);
	}
	mysql_real_escape_string(mysql, esc, origin, originlen);

	/* Construct query */
	if (mydns_soa_use_active)
		querylen = snprintf(query, sizeof(query),
			"SELECT "MYDNS_SOA_FIELDS" FROM %s WHERE origin='%s' AND active=1",
			mydns_soa_table_name, esc);
	else
		querylen = snprintf(query, sizeof(query),
			"SELECT "MYDNS_SOA_FIELDS" FROM %s WHERE origin='%s'", mydns_soa_table_name, esc);

	/* Submit query */
	if (mysql_real_query(mysql, query, querylen) || !(res = mysql_store_result(mysql)))
		return (-1);

	/* Add results to list */
	while ((row = mysql_fetch_row(res)))
	{
		MYDNS_SOA *new;

		if (!(new = mydns_parse_soa(row)))
		{
			mysql_free_result(res);
			return (-1);
		}
		if (!first) first = new;
		if (last) last->next = new;
		last = new;
	}
	*rptr = first;
	mysql_free_result(res);
	return (0);
}
/*-----------------------------------------------------------------------------------------------*/

/* vi:set ts=3: */
